package net.cathackers.devel.scmp.chat.factories;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.chat.impl.OnlineSessionsCacheImpl;
import net.cathackers.devel.scmp.chat.impl.groupchat.GroupChatManagerImpl;
import net.cathackers.devel.scmp.chat.impl.sockets.SocketServerImpl;
import net.cathackers.devel.scmp.chat.impl.sockets.SocketServerMonitorImpl;
import net.cathackers.devel.scmp.chat.impl.xmpp.iqcomponent.IQComponent;
import net.cathackers.devel.scmp.chat.interfaces.ChatManager;
import net.cathackers.devel.scmp.chat.interfaces.GroupChatManager;
import net.cathackers.devel.scmp.chat.interfaces.OnlineSessionsCache;
import net.cathackers.devel.scmp.chat.interfaces.Protocol;
import net.cathackers.devel.scmp.chat.interfaces.sockets.SocketServer;
import net.cathackers.devel.scmp.chat.interfaces.sockets.SocketServerMonitor;
import net.cathackers.devel.scmp.factories.SCMPFactory;

public class ChatServiceFactory {
    private static volatile String		lock_           = "Hello Locking";
    private static volatile OnlineSessionsCache	onlineSessionsCache_	= null;
    private static volatile IQComponent	iqComponent_	                = null;
    private static volatile GroupChatManager	groupChatManager_	= null;
    
    private static Logger getLogger_() {
	return SCMPFactory.getLogger(ChatServiceFactory.class);
    }

//    public static LobbyUser newLobbyUser(Socket socket, Protocol protocol) throws CATException{
//	switch (protocol) {
//	case Jabber:
//	    return new XMPPLobbyUser(socket, protocol);
//	default:
//	    throw new CATException("Unknown Protocol, cannot instantiate LobbyUser");
//	}
//    }
    public static SocketServer newSocketServer(Protocol protocol, ChatManager manager) {
	return new SocketServerImpl(protocol, manager);
    }

    public static SocketServerMonitor newSocketServerMonitor(SocketServer socketServer) {
	return new SocketServerMonitorImpl(socketServer);
    }
    
//    @SuppressWarnings("unchecked")
//    public static ChatStorageBackend newChatStorageBackend(
//	    ChatConfiguration config) {
//	
//	ChatStorageBackend ret = null;
//	try {
//	    getLogger_().debug("Trying to dynamically instantiate object of " + config.getChatStorageBackendClassName());
//	    Class storageBackendClass = Class.forName(config.getChatStorageBackendClassName());
//	    ret = (ChatStorageBackend) storageBackendClass.newInstance();
//	} catch (Exception ex) {
//	    // log that and return null.
//	    getLogger_().error("Cannot Instantiate " + config.getChatStorageBackendClassName() + " Object:" + ex.getMessage());
//	    ret = null;
//	}
//	return ret;
//    }

    /*public static OnlineSessionsCache newChatUsersCache() {
	return new OnlineSessionsCacheImpl();
    }*/
    
    public static OnlineSessionsCache getOnlineSessionsCache() {
        if ( onlineSessionsCache_ == null) {
            synchronized (lock_) {
                if (onlineSessionsCache_ == null) {
                    onlineSessionsCache_ = new OnlineSessionsCacheImpl();
                }
            }
        }
        return  onlineSessionsCache_;
    }
    
    public static IQComponent getIQComponent() {
        if (iqComponent_ == null) {
            synchronized (lock_) {
                if (iqComponent_ == null) {
                    iqComponent_ = new IQComponent();
                }
            }
        }
        return iqComponent_;
    }
    
    public static GroupChatManager getGroupchatManager() {
        if (groupChatManager_ == null) {
            synchronized (lock_) {
                if (groupChatManager_ == null) {
                    groupChatManager_ = new GroupChatManagerImpl();
                }
            }
        }
        return groupChatManager_;
    }

    
} 
